package kjh820909.swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;
import javax.swing.JCheckBox;

import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

import javax.swing.JRadioButton;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import java.awt.Dimension;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class BComp extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtBtn;
	private JPasswordField pwdBtn;
	private JList list1;
	private JList list2;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			BComp dialog = new BComp();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public BComp() {
		setTitle("기본 컴퍼넌트");
		setBounds(100, 100, 640, 480);
		setLocationRelativeTo(null);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setLayout(new FlowLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		{
			JButton imgBtn = new JButton("이미지 버튼");
			imgBtn.setToolTipText("이미지 버튼입니다");
			imgBtn.setIcon(new ImageIcon(BComp.class.getResource("/kjh820909/swing/images/icon/icon.png")));
			contentPanel.add(imgBtn);
		}
		{
			JLabel imgLabel = new JLabel("이미지레이블");
			imgLabel.setIcon(new ImageIcon(BComp.class.getResource("/kjh820909/swing/images/img/icon2.png")));
			contentPanel.add(imgLabel);
		}
		{
			JCheckBox chkSu = new JCheckBox("수지");
			chkSu.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					JCheckBox c = (JCheckBox) e.getSource();
					if(c.isSelected())
						txtBtn.setText(c.getText());
					else
						txtBtn.setText( "" );
				}
			});
			contentPanel.add(chkSu);
		}
		{
			JCheckBox chkHye = new JCheckBox("혜교");
			chkHye.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					JCheckBox c = (JCheckBox) e.getSource();
					if(c.isSelected())
						txtBtn.setText(c.getText());
					else
						txtBtn.setText( "" );
				}
			});
			contentPanel.add(chkHye);
		}
		{
			JCheckBox chkJi = new JCheckBox("지현");
			chkJi.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					JCheckBox c = (JCheckBox) e.getSource();
					if(c.isSelected())
						txtBtn.setText(c.getText());
					else
						txtBtn.setText( "" );
				}
			});
			contentPanel.add(chkJi);
		}
		// 라디오 버튼 선언 시작
			JRadioButton radSu = new JRadioButton("수지");
			radSu.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					JRadioButton r =
							(JRadioButton)e.getSource();
					if(r.isSelected())
						txtBtn.setText(r.getText());
					else
						txtBtn.setText( "" );
				}
			});
			contentPanel.add(radSu);
		
		
			JRadioButton radHye = new JRadioButton("혜교");
			radHye.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					JRadioButton r =
							(JRadioButton)e.getSource();
					if(r.isSelected())
						txtBtn.setText(r.getText());
					else
						txtBtn.setText( "" );
				}
			});
			contentPanel.add(radHye);
		
		
			JRadioButton radJi = new JRadioButton("지현");
			radJi.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					JRadioButton r =
							(JRadioButton)e.getSource();
					if(r.isSelected())
						txtBtn.setText(r.getText());
					else
						txtBtn.setText( "" );
				}
			});
			contentPanel.add(radJi);
		
		// 라디오 버튼 선언 끝
		// 라디오 버튼 그룹 생성 및 추가
		ButtonGroup rgroup = new ButtonGroup();
		rgroup.add(radSu);
		rgroup.add(radHye);
		rgroup.add(radJi);
		{
			txtBtn = new JTextField();
			contentPanel.add(txtBtn);
			txtBtn.setColumns(10);
		}
		{
			JButton txt1Btn = new JButton("입력문자보기");
			txt1Btn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JOptionPane.showMessageDialog(null, txtBtn.getText());
				// textField에 입력된 문자 출력
					
				}
			});
			contentPanel.add(txt1Btn);
		}
		{
			pwdBtn = new JPasswordField();
			pwdBtn.setEchoChar('♡');
			pwdBtn.setColumns(16);
			contentPanel.add(pwdBtn);
		}
		{
			JButton pwd1Btn = new JButton("입력문자보기");
			pwd1Btn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JOptionPane.showMessageDialog(null, pwdBtn.getText());
				//  pwdBtn.getPassword()와 비교
				}
			});
			contentPanel.add(pwd1Btn);
		}
		{
			JScrollPane scrollPane1 = new JScrollPane();
			scrollPane1.setPreferredSize(new Dimension(150, 100));
			contentPanel.add(scrollPane1);
			{
				// JList에 추가할 항목을 배열로 생성
				String[] data =
					{"수지", "혜교", "지현"
						,"태연", "혜선", "순규"};
				// 생성된 배열을 JList의 생성자에 주입
				list1 = new JList(data);
				scrollPane1.setViewportView(list1);
			}
		}
		{
			JScrollPane scrollPane2 = new JScrollPane();
			scrollPane2.setPreferredSize(new Dimension(150, 100));
			contentPanel.add(scrollPane2);
			{
				list2 = new JList();
				list2.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						if(e.getClickCount()==2){
							JOptionPane.showMessageDialog(null, list2.getSelectedIndex() + "/"
									+ list2.getSelectedValue());
						}
					}
				});
				list2.setModel(new AbstractListModel() {
					String[] values = new String[] {"사과", "배", "수박", "키위", "바나나", "오렌지", "참외", "메론", "자몽"};
					public int getSize() {
						return values.length;
					}
					public Object getElementAt(int index) {
						return values[index];
					}
				});
				scrollPane2.setViewportView(list2);
			}
		}
		{
			JButton listBtn = new JButton("리스트선택보기");
			listBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, list1.getSelectedIndex() + "/"
									+ list1.getSelectedValue());
				
				}
			});
			contentPanel.add(listBtn);
		}
		{
			JScrollPane scrollPane3 = new JScrollPane();
			scrollPane3.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
			scrollPane3.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
			scrollPane3.setPreferredSize(new Dimension(250, 200));
			contentPanel.add(scrollPane3);
			{
				JTextArea textArea = new JTextArea();
				textArea.setLineWrap(true);
				scrollPane3.setViewportView(textArea);
			}
		}
		{
			String[] data = {
					"쥐","소","돼지","말","개","용","고양이","호랑이","사자","코끼리","코알라"
			};
			final JComboBox comboBox1 = new JComboBox(data);
			comboBox1.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					JOptionPane.showMessageDialog(null, comboBox1.
							getSelectedIndex()+ "/"
							+ comboBox1.getSelectedItem());
				}
			});
			contentPanel.add(comboBox1);
		}
		{
			JComboBox comboBox2 = new JComboBox();
			comboBox2.setModel(new DefaultComboBoxModel(new String[] {"혜교", "지현", "사랑", "유리", "연아", "연재"}));
			contentPanel.add(comboBox2);
		}
		
			
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
		}
	}

}
